package com.atguigu.commonutils;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author:张鹏
 * @description: 更详细的记录错误信息
 * @date: 2022/7/16 11:34
 */

public class ExceptionUtil {
    public static String getMessage(Exception e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();

            pw = new PrintWriter(sw);

// 将出错的栈信息输出到printWriter中
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();

        } finally {

            if (sw != null) {

                try {
                    sw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (pw != null) {
                pw.close();
            }
        }
        return sw.toString();
    }

}
